package com.thinkfit.jy.utils;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.widget.TextView;

import com.common.lib.manager.DataManager;
import com.common.lib.utils.LogUtil;
import com.thinkfit.jy.R;
import com.thinkfit.jy.serialport.SerialPortManager;

import java.util.Locale;

public class Utils {

    private static void changeAppLanguage(Resources resources, Locale language) {
        Configuration configuration = resources.getConfiguration();
        DisplayMetrics displayMetrics = resources.getDisplayMetrics();
        configuration.setLocale(language);
        resources.updateConfiguration(configuration, displayMetrics);

        LogUtil.LogE("language: "+language);
    }

    public static void changeAppLanguage(Context context, int language) {
        Locale locale = Locale.SIMPLIFIED_CHINESE;
        switch (language) {
            case 0:
                locale = Locale.SIMPLIFIED_CHINESE;
                break;
            case 1:
                locale = Locale.ENGLISH;
                break;
        }
        changeAppLanguage(context.getResources(), locale);
    }

    public static String getNewFloat(float number) {
        return String.format("%.1f", number + 0.00001);
    }

    public static String getNewFloat2(float number) {
        return String.format("%.2f", number + 0.00001);
    }

    public static String removeZero(String number) {
        if (TextUtils.isEmpty(number)) {
            return "0";
        }
        if (number.indexOf(".") > 0) {
            number = number.replaceAll("0+?$", "");//去掉后面无用的零
            number = number.replaceAll("[.]$", "");//如小数点后面全是零则去掉小数点
        }
        if (number.contains(".")) {
            String t = number.split("\\.")[1];
            return (t != null && t.length() > 1) ? String.format("%.1f", Double.parseDouble(number)) : number;
        } else {
            return number;
        }
    }

    public static String removeZero2(String number) {
        if (TextUtils.isEmpty(number)) {
            return "0";
        }
        if (number.indexOf(".") > 0) {
            number = number.replaceAll("0+?$", "");//去掉后面无用的零
            number = number.replaceAll("[.]$", "");//如小数点后面全是零则去掉小数点
        }
        if (number.contains(".")) {
            String t = number.split("\\.")[1];
            return (t != null && t.length() > 2) ? String.format("%.2f", Double.parseDouble(number)) : number;
        } else {
            return number;
        }
    }

    public static void startSports(Context context,boolean isVideo){
        if(DataManager.Companion.getInstance().isLogin()){
            if(DeviceTypeUtils.getDeviceType() == 2){
                if(DeviceTypeUtils.connectDeviceType() == 0){
                    if(DeviceTypeUtils.getInstance().isTreadmill()){
                        BluetoothOrder.getInstance().startTreadmill();
                    }else {
                        BluetoothOrder.getInstance().startPlay();
                    }
                }else {
                    SerialPortManager.getInstance().startRun();
                }
            }else {
                CountDownViewUtils.getInstance().initView(context,3);
            }
//                    CountDownViewUtils.getInstance().initView(mContext,3);
        }else {
            DialogViewUtils.getInstance().initView(context,false,DialogViewUtils.TOURIST_PATTERN);
        }
        CountDownViewUtils.getInstance().setIsVideo(isVideo);
    }

    public static void initRegion(TextView tvRegion,TextView tvLanguage,TextView tvUnit){
        int region = DataManager.Companion.getInstance().getRegion();
        int language = DataManager.Companion.getInstance().getLanguage();;
        boolean unit = DataManager.Companion.getInstance().getUnit();
        if (region == 0) {
            tvRegion.setText(R.string.app_asia);
        } else if (region == 1) {
            tvRegion.setText(R.string.app_america);
        } else {
            tvRegion.setText(R.string.app_europe);
        }

        if (language == 0) {
            tvLanguage.setText(R.string.app_sc);
        } else if (language == 1){
            tvLanguage.setText(R.string.app_en);
        }else {
            tvLanguage.setText(R.string.app_tc);
        }
        if(unit){
            tvUnit.setText(R.string.app_imperial_units);
        }else{
            tvUnit.setText(R.string.app_metric_units);
        }
    }

}
